<?php             
class Taxonomy_item extends MY_Controller{				
	function __construct()
	{
		parent::__construct();
	}
	function _remap($method,$params = array())
	{
	   $this->_admin_remap($method,$params);	   
	}
        
    function index($taxonomy_id){
        $this->load->model('Language');
        $data = array();
        foreach($this->Language->get_all() as $row){
                $data['langList'][$row['language_code']] = $row['language_name'];
        }
        $data['taxonomy_id'] = $taxonomy_id;
    	$this->_setLayout('admin/taxonomy_item/taxonomy_item_list','admin_layout',$data);
    }
            
	function add(){
		$callback = $_REQUEST['callback'];
		$jsonOutPut = '';
		try{
			$data = json_decode ($_GET['data'],true);
			if(empty($data)){
				$jsonOutPut = json_encode(array('success'=>false,'message'=>'Can not save'));	    			    		
			}
			else{
				$this->load->model('Taxonomy_item_model');
				if(isset($data['id'])){
					$this->Taxonomy_item_model->update($data,$data['id']);
					$data_return = $this->Taxonomy_item_model->get_by_id($data['id']);
					$data_return['edit'] = '<a href="'.base_url().'admin/taxonomy_item/edit/'.$data['id'].'"><img src="'.base_url().'public/skin/back-end/images/edit.png"></a>';
					$jsonOutPut = json_encode(array('success'=>true,'message'=>'Saved successfully','data'=>$data_return));
				}	
				else{	
					$id = $this->Taxonomy_item_model->insert($data);
					$data_return = $this->Taxonomy_item_model->get_by_id($id);
					$data_return['edit'] = '<a href="'.base_url().'admin/taxonomy_item/edit/'.$id.'"><img src="'.base_url().'public/skin/back-end/images/edit.png"></a>';
					$jsonOutPut = json_encode(array('success'=>true,'message'=>'Loaded data','data'=>$data_return));
				}		 
			}
		}catch (Exception $e){
			$jsonOutPut = json_encode(array('success'=>false,'message'=>'Can not save'));	
		}
		if ($callback) {
			header('Content-Type: text/javascript');
			echo $callback . '(' . $jsonOutPut . ');';
		} else {
			header('Content-Type: application/x-json');
			echo $jsonOutPut;
		}	
	}
			
	function delete(){
		$callback = $_REQUEST['callback'];
		$jsonOutPut = '';
		try{
			$data = json_decode ($_GET['data'],true);			
			$this->load->model('Taxonomy_item_model');
			$this->Taxonomy_item_model->delete_all_child($data);
			$jsonOutPut =  json_encode(array('success'=>true));
		}
		catch (Exception $e){
			$jsonOutPut =  json_encode(array('success'=>false));
		}
		if ($callback) {
			header('Content-Type: text/javascript');
			echo $callback . '(' . $jsonOutPut . ');';
		} else {
			header('Content-Type: application/x-json');
			echo $jsonOutPut;
		}	
	}
			
	function grid(){
		$this->load->model('Taxonomy_item_model');
		$callback = $_REQUEST['callback'];
		$data =  array();
                $treeResult = $this->Taxonomy_item_model->getAllTree($_GET['taxonomy_id'],"---",true,2);               
		foreach($treeResult as $row){
			$data[] = array_merge($row,array('edit'=>'<a href="'.base_url().'admin/taxonomy_item/edit/'.$row['id'].'/'.$_GET['taxonomy_id'].'"><img src="'.base_url().'public/skin/back-end/images/edit.png"></a>'));
		}
		$jsonOutPut = json_encode(array('success'=>true,'message'=>'Loaded data','data'=>$data));
		if ($callback) {
			header('Content-Type: text/javascript');
			echo $callback . '(' . $jsonOutPut . ');';
		} else {
			header('Content-Type: application/x-json');
			echo $jsonOutPut;
		}
	}
			
	function edit($id=0,$taxonomy_id)
	{
            $this->load->model('Taxonomy_item_model');
            $this->load->model('Language');
            $this->load->library('helpfunc');
            $currentNode = (empty($id))?array():$this->Taxonomy_item_model->get_by_id($id);
            $prexNode = (empty($id))?array():$this->Taxonomy_item_model->getNodebyRight($currentNode['lft']-1,$taxonomy_id);
            $parentNode = (empty($id))?array():$this->Taxonomy_item_model->getParent($id);
            $nextNode = (empty($id))?array():$this->Taxonomy_item_model->getNodebyLeft($currentNode['rgt']+1,$taxonomy_id);
            $allchildarr = (empty($id))?array():$this->Taxonomy_item_model->getallchild($id,$taxonomy_id);
            $result = array();
            foreach ($allchildarr as $item)  array_push ($result, $item['id']);
            $allchildarr = $result;

            $data = array(
                'datacontent' => $currentNode,
                'message' => null,
                'preNode' => $prexNode,
                'nextNode' => $nextNode,
                'parentNode' => $parentNode,
                'allchildarr' =>  $allchildarr
            );
            if($this->input->server('REQUEST_METHOD') == 'POST'){
                    $input_data = $_POST;
                    if(!empty($id) && $this->Taxonomy_item_model->update($input_data,$id)){
                            $data['message'] = 'Update successfully';
                            $data['datacontent'] = array_merge($data['datacontent'],$input_data);
                    }elseif($id = $this->Taxonomy_item_model->insert($input_data,$id)){
                            $data['message'] = 'Update successfully';
                            $data['datacontent'] = $this->Taxonomy_item_model->get_by_id($id);
                    }
                    else
                            $data['message'] = 'Update fail';

            }
            foreach($this->Language->get_all() as $row){
                    $data['langList'][$row['language_code']] = $row['language_name'];
            }
            //Get related Model
            $this->load->model('Taxonomy_model');
            $relateModel = $this->Taxonomy_model->getRelatedModel($taxonomy_id);
            $this->load->model($relateModel);
            
            $relateData = $this->$relateModel->getRelatedComponent();
            $data['taxonomy_id'] = $taxonomy_id;
            $data['related_data'] = $relateData;
            $taxonomylist = $this->Taxonomy_item_model->get_by_field(array('taxonomy_id'=>$taxonomy_id));
            foreach ($taxonomylist as $item){
                $parent_option[$item['id']] = $item['title'];
            }
            $data['parent_id'] = 0;
            $data['parent_option'] = $parent_option;
            $data['alltree'] = $this->Taxonomy_item_model->getAllTree($taxonomy_id);
            $this->_setLayout('admin/taxonomy_item/taxonomy_item_edit', 'admin_layout', $data);
	}
          //ajax
        function getchild(){
            if (!isset($_GET['current_id'])) $_GET['current_id'] = null;
            if (!isset($_GET['prenode_id'])) $_GET['prenode_id'] = -1;
            if ($_GET['prenode_id'] == -1 && isset($_GET['nextnode_id'])) $select = "selected = 'selected'";
            else $select = "";
            $this->load->model('Taxonomy_item_model');
            $data = $this->Taxonomy_item_model->getchild($_GET['id'],$_GET['taxonomy_id'],$_GET['current_id']);
            if (count($data)==0) die;
            $str = "<th scope=\"row\">Position</th>
            <td align=\"left\">
                <select name='position'>
                    <option  value='after'>after</option>
                    <option {$select} value='before'>before</option>
                </select> - <select name='position_item'>";
            foreach ($data as $item){
                if ($_GET['prenode_id'] == $item['id'])
                    $str .= "<option selected='selected' value='{$item['id']}'>{$item['title']}</option>";
                else
                    $str .= "<option value='{$item['id']}'>{$item['title']}</option>";
            }
                $str .= "</select></td>";
            echo $str;
        }
}
		 ?>